package com.tcu.part.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tcu.exception.SmartException;
import com.tcu.part.entity.PageInfo;
import com.tcu.part.entity.Part;
import com.tcu.part.entity.vo.PartVo;
import com.tcu.part.service.IPartService;
import com.tcu.result.R;
import org.mockito.internal.matchers.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/partInfo")
public class PartController {
    private IPartService partService;

    /**
     * TODO: 在增加零件种类的同时，要增加对应的零件数目 对象。
     * @param partService
     */
    @Autowired
    public void setPartService(IPartService partService) {
        this.partService = partService;
    }
    /**
     * 需要的功能有， 增删改查 ，列表 ，条件分页查询；
     */
    @PostMapping("add")
    public R add(@RequestBody Part part){
        if(part.getId()!=null){
            throw new SmartException(20001,"增加零件信息的id必须不为空");
        }
        boolean save = partService.save(part);
        return save ? R.success().data("part",part) : R.error();
    }
    @DeleteMapping("delete/{id}")
    public R deleteById(@PathVariable String id){
        boolean b = partService.removeById(id);
        return b ? R.success() : R.error();
    }
    @PostMapping("deleteByBatchIds")
    public R deleteBatch(@RequestBody List<String> ids){
        if (ids.size()==0) {
            throw new SmartException(20001,"id集合为空，无法删除");
        }
        boolean b = partService.removeByIds(ids);
        return b ? R.success() : R.error();
    }

    @PostMapping("update")
    public R updatePart(@RequestBody Part part){
        if (part.getId()==null) {
            throw new SmartException(20001,"未指定更新的数据");
        }
        boolean b = partService.updateById(part);
        return b ? R.success() : R.error();
    }
    @GetMapping("getOne/{id}")
    public R getOne(@PathVariable String id){
        Part byId = partService.getById(id);
        return R.success().data("part", byId);
    }

    @PostMapping("list/{current}/{size}")
    public R pageListCondition(@PathVariable Long current, @PathVariable Long size, @RequestBody PartVo partVo){
        IPage<Part> partPage = new Page<>(current,size);

        QueryWrapper<Part> wrapper = new QueryWrapper<>();
        NotNull notNull = NotNull.NOT_NULL;

        wrapper.like(notNull.matches(partVo.getNameLike()),"name", partVo.getNameLike())
                .between(notNull.matches(partVo.getStartPrice())&& notNull.matches(partVo.getEndPrice()),"single_price",partVo.getStartPrice(),partVo.getEndPrice())
                .eq(notNull.matches(partVo.getPartTypeId()), "part_type_id",partVo.getPartTypeId())
                .like(notNull.matches(partVo.getManufacturerLike()),"manufacturer",partVo.getManufacturerLike())
                .ge(notNull.matches(partVo.getCreateTime()),"production_time",partVo.getCreateTime() );
        partService.page(partPage,wrapper);
        PageInfo p = new PageInfo(partPage.getCurrent(),partPage.getSize(),partPage.getTotal(),partPage.getPages());
        return R.success().data("list",partPage.getRecords()).data("pageInfo",p);
    }




}
